{% extends "bash.html" %}
{% load static %}
{% block content %}

    <form action="{% url 'index:index' %}" method="get" id="myform">
    {% csrf_token %}
        <div class="input-group line left">
            <span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
            {{ infos.s }}
        </div>

        {{ infos.n }}
        <div class="input-group line left">
            <span class="input-group-addon" id="basic-addon2"><i class="icon-edit"></i></span>
            {{ infos.t }}
        </div>

        <button class="btn btn-primary" type="submit" form="myform"><span class="glyphicon glyphicon-search"></span></button>
    </form>

    <br/><br/>
    <table class="table table-bordered table-striped text-center bg-info" id="show_number">
    <thead>
    <tr class="info">

        <th class="text-center">资产编号</th>
        <th class="text-center"> 使用者 </th>
        <th class="text-center">资产名称</th>
        <th class="text-center">具体位置</th>
        <th class="text-center">资产类别</th>
        <th class="text-center">备   注</th>
        <th class="text-center">盘点状态</th>
        <th class="text-center">操作</th>
    </tr>
    </thead>
    <tbody>
     {% for in_show_value in pages.object_list %}
        <tr>

            <td id="{{ in_show_value.0 }}_1">{{ in_show_value.0 }}</td>
            <td id="{{ in_show_value.0 }}_2">{{ in_show_value.1 }}</td>
            <td id="{{ in_show_value.0 }}_3">{{ in_show_value.2 }}</td>
            <td id="{{ in_show_value.0 }}_4">{{ in_show_value.3 }}</td>
            <td id="{{ in_show_value.0 }}_5">{{ in_show_value.4}}</td>
            <td id="{{ in_show_value.0 }}_8">{{ in_show_value.6}}</td>
            <td id="{{ in_show_value.0 }}_7" hidden="hidden">{{ in_show_value.5}}</td>
            {% if in_show_value.7 == "1" %}
                <td id="{{ in_show_value.0 }}_6">已确认</td>
            {% else %}
                <td id="{{ in_show_value.0 }}_6"></td>
            {% endif %}

            <td>
                <a href="#" class="btn btn-primary btn-sm" id="{{ in_show_value.0 }}" onclick="statusok(this)">确认</a>

                <a href="#" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#edit"data-whatever="{{ in_show_value.0 }}" >编辑</a>
                <!--确认键用于改变盘点状态-->
                <a href="#" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#add"data-whatever="添加" >添加</a>
                <a href="#" class="btn btn-primary btn-sm" id="{{ in_show_value.0 }}" onclick="crap(this)">报废</a>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>
     <div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="edit">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel1">编辑</h4>
                            </div>
                            <div class="modal-body">
                               <div>
                                    <div>
                                        <label><span>资产编号</span></label>
                                        <input type="text" placeholder="**必填" id="edit_number" readonly="readonly"/>
                                    </div>
                                    <div>
                                        <label><span>资产名称</span></label>
                                        <input type="text" placeholder="电脑 or 笔记本 or 手机 or 家具 or 其他" id="edit_name"/>
                                    </div>
                                    <div>
                                        <label><span>资产型号</span></label>
                                        <input type="text" placeholder="具体型号" id="edit_category"/>
                                    </div>
                                    <div>
                                        <label><span>数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;量</span></label>
                                        <input type="text" placeholder="数量建填写1" id="edit_num"/>
                                    </div>
                                    <div>
                                        <label><span>资产分类</span></label>
                                        <input type="text" placeholder="**必填 0:笔记本 电脑 手机,1:其他" id="edit_table" readonly="readonly" />
                                    </div>
                                    <div>
                                        <label><span>具体位置</span></label>
                                        <input type="text" placeholder="**必填" id="edit_seat_number"/>
                                    </div>
                                    <div>
                                        <label><span>使用姓名</span></label>
                                        <input type="text" placeholder="选填" id="edit_asset_users"/>
                                    </div>
                                    <div>
                                        <label><span>备  注</span></label>
                                        <input type="text" id="edit_remarks"/>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary btn-sm" data-dismiss="modal" onclick="editUser()">保存</button>
                            </div>
                        </div>
                    </div>
                </div>
    <!--添加-->
                <div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="add11">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" >添加</h4>
                            </div>
                            <div class="modal-body">
                                <ul>
                                    <li>
                                        <label><span>资产编号</span></label>
                                        <input type="text" placeholder="**必填" id="add_number"/>
                                    </li>
                                    <li>
                                        <label><span>资产名称</span></label>
                                        <input type="text" placeholder="电脑 or 笔记本 or 手机 or 家具 or 其他" id="add_name"/>
                                    </li>
                                    <li>
                                        <label><span>资产信号</span></label>
                                        <input type="text" placeholder="具体型号" id="add_category"/>
                                    </li>
                                    <li>
                                        <label><span>数    量</span></label>
                                        <input type="text" placeholder="数量建填写1" id="add_num"/>
                                    </li>
                                    <li>
                                        <label><span>资产分类</span></label>
                                        <input type="text" placeholder="**必填数字 0:笔记本 电脑 手机,1:其他" id="add_table"/>
                                    </li>
                                    <li>
                                        <label><span>具体位置</span></label>
                                        <input type="text" placeholder="**必填" id="add_seat_number"/>
                                    </li>
                                    <li>
                                        <label><span>使 用 者</span></label>
                                        <input type="text" placeholder="选填" id="add_asset_users"/>
                                    </li>
                                    <li>
                                        <label><span>备   注</span></label>
                                        <input type="text" id="add_remarks"/>
                                    </li>
                                </ul>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary btn-sm" onclick="UpdataUser()">保存</button>
                            </div>
                        </div>
                    </div>
                </div>
    <div>
    <ul class="pagination pagination-lg right">
        {% if pages.has_previous %}
            <li><a href="{% url 'index:index' %}?p={{ pages.number|add:'-1' }}&s={{ infos.s.value }}&n={{ infos.n.value }}&t={{ infos.t.value }}">&laquo;</a></li>
        {% endif %}
        {% for page in pages.paginator.page_range %}
            {% if pages.number == page %}
                <li class="active"><a href="{% url 'index:index' %}?&p={{ page }}&s={{ infos.s.value }}&n={{ infos.n.value }}&t={{ infos.t.value }}">{{ page }}</a></li>
            {% elif pages.number|add:'-1' == page or pages.number|add:'1' == page%}
                <li><a href="{% url 'index:index' %}?&p={{ page }}&s={{ infos.s.value }}&n={{ infos.n.value }}&t={{ infos.t.value }}">{{ page }}</a></li>
            {% elif pages.number|add:'-2' == page or pages.number|add:'2' == page%}
                <li><a href="{% url 'index:index' %}?&p={{ page }}&s={{ infos.s.value }}&n={{ infos.n.value }}&t={{ infos.t.value }}">{{ page }}</a></li>
            {% endif %}
        {% endfor %}
        {% if pages.has_next %}
            <li><a href="{% url 'index:index' %}?p={{ pages.number|add:'1' }}&s={{ infos.s.value }}&n={{ infos.n.value }}&t={{ infos.t.value }}">&raquo;</a></li>
        {% endif %}
    </ul>
    </div>

{% endblock content %}


{% block script %}
    $.ajaxSetup({
            data:{csrfmiddlewaretoken:'{{ csrf_token }}'}
        });
   /* $().ready(function (){
      $.post("{% url 'index:index' %}",function(data,status){
        alert("正确加载")

    });
    });*/

    $('#add').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var recipient = button.data('whatever');

        var modal = $(this);
        modal.find('.modal-title').text('' + recipient);
    });

    $('#edit').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var recipient = button.data('whatever');// Extract info from data-* attributes
        // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
        // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
        var modal = $(this);
        modal.find('.modal-title').text('编辑：' + recipient);
        modal.find('.modal-body input').val("")
        /* 资产编号 edit_number
        *  资产名称 edit_name
        *  资产型号 edit_category
        *  数    量 edit_num
        *  资产分类 edit_table
        *  具体位置 edit_seat_number
        *  使 用 者 edit_asset_users
        *  备    注 edit_remarks
        */
        // 添加初始化数据框
        document.getElementById("edit_number").value = document.getElementById(""+recipient+"_1").innerText;
        document.getElementById("edit_asset_users").value = document.getElementById(""+recipient+"_2").innerText;
        document.getElementById("edit_name").value = document.getElementById(""+recipient+"_3").innerText;
        document.getElementById("edit_seat_number").value = document.getElementById(""+recipient+"_4").innerText;
        document.getElementById("edit_category").value = document.getElementById(""+recipient+"_5").innerText;
        document.getElementById("edit_table").value = document.getElementById(""+recipient+"_7").innerText;
        document.getElementById("edit_remarks").value = document.getElementById(""+recipient+"_8").innerText;

    });

    function editUser(){
        /* 资产编号 edit_number
        *  资产名称 edit_name
        *  资产型号 edit_category
        *  数    量 edit_num
        *  资产分类 edit_table
        *  具体位置 edit_seat_number
        *  使 用 者 edit_asset_users
        *  备    注 edit_remarks
        */
        var number = document.getElementById("edit_number").value;
        var name = document.getElementById("edit_name").value;
        var category = document.getElementById("edit_category").value;
        var num = document.getElementById("edit_num").value;
        var table = document.getElementById("edit_table").value;
        var seat_number = document.getElementById("edit_seat_number").value;
        var asset_users = document.getElementById("edit_asset_users").value;
        var remarks = document.getElementById("edit_remarks").value;
        var in_tab = $("#sel_table option:selected").text();
        $.post("{% url 'index:edit' %}",{'number':number,'name':name,'category':category,'num':num,'table':table,'seat_number':seat_number,'asset_users':asset_users,'remarks':remarks,'in_tab':in_tab},function(data,status){
            alert(data.msg)
        });
        document.getElementById(""+number+"_1").innerText = number;
        document.getElementById(""+number+"_2").innerText = asset_users;
        document.getElementById(""+number+"_3").innerText = name;
        document.getElementById(""+number+"_4").innerText = seat_number;
        document.getElementById(""+number+"_5").innerText = category;
        document.getElementById(""+number+"_8").innerText = remarks;
    }

    function UpdataUser() {
        /* 资产编号 add_number
        *  资产名称 add_name
        *  资产型号 add_category
        *  数    量 add_num
        *  资产分类 add_table
        *  具体位置 add_seat_number
        *  使 用 者 add_asset_users
        *  备    注 add_remarks
        * edit_table 为0时候插入电子表，为1时插入家具表
        */
        var number = document.getElementById("add_number").value;
        var name = document.getElementById("add_name").value;
        var category = document.getElementById("add_category").value;
        var num = document.getElementById("add_num").value;
        var table = document.getElementById("add_table").value;
        var seat_number = document.getElementById("add_seat_number").value;
        var asset_users = document.getElementById("add_asset_users").value;
        var remarks = document.getElementById("add_remarks").value;
        var in_tab = $("#sel_table option:selected").text();
        if (num == ""){
            num = 1;
        }
        if (table == "0"){
            table ="computerstatus_ipassets"
        }else if (table == "1"){
            table = "computerstatus_oassets"
        }else{
            alert("资产分类输入错误！")
            return -1
        }
        if (number == ""){
           alert("请输出资产编号")
            return -1
        }
        if (seat_number == ""){
           alert("请输入具体位置")
            return -1
        }
        $.post("{% url 'computerstatus:save' %}",{'number':number,'name':name,'category':category,'num':num,
        'table':table,'seat_number':seat_number,'asset_users':asset_users,'remarks':remarks,'in_tab':in_tab},function(data,status){
            alert(data.msg)
        });

        $("#add").find('.modal-body input').val("")
    }


    function statusok(obj){
        var number = obj.id
        $.post("{% url 'index:ok'%}",{'it_number':number})
        document.getElementById(""+number+"_6").innerText = "已确认";
    }

    function crap(obj){
        var number = obj.id
        var in_tab = $("#sel_table option:selected").text();
        var table = document.getElementById(""+number+"_7").innerText;
        var leibie = document.getElementById(""+number+"_3").innerText;
        if(window.confirm('你确定要报废：'+obj.id+"吗？")){
            $.post("{% url 'index:crap' %}",{'it_number':number,'in_tab':in_tab,'table':table,'leibie':leibie},function(data,status){
                window.location.reload(data);
        });

        }
    }

{% endblock script %}